<?php

namespace Modules\Wx\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Modules\Wx\Entities\Wxconfig;
use Modules\Wx\Http\Requests\WxconfigRequest;

class WxconfigController extends Controller
{
    //显示列表
    public function index()
    {
        $data = Wxconfig::paginate(10);
        return view('wx::wxconfig.index', compact('data'));
    }

    //创建视图
    public function create(Wxconfig $wxconfig)
    {
        $wxconfig = $wxconfig->query()->pluck('value', 'name');
        return view('wx::wxconfig.create', compact('wxconfig'));
    }

    //保存数据
    public function store(WxconfigRequest $request, Wxconfig $wxconfig)
    {
        $wxconfig->query()->truncate();//先清除所有数据，保证数据不重复
        foreach ($request -> except('_token') as $name => $value) {
            $wxconfig->query()->insert(['name' => $name, 'value' => $value]);
        }
        return redirect('/wx/wxconfig/create')->with('success', '更新成功');
    }

    //显示记录
    public function show(Wxconfig $field)
    {
        return view('wx::wxconfig.show', compact('field'));
    }

    //编辑视图
    public function edit(Wxconfig $wxconfig)
    {
        return view('wx::wxconfig.edit', compact('wxconfig'));
    }

    //更新数据
    public function update(WxconfigRequest $request, Wxconfig $wxconfig)
    {
        $wxconfig->update($request->all());
        return redirect('/wx/wxconfig')->with('success', '更新成功');
    }

    //删除模型
    public function destroy(Wxconfig $wxconfig)
    {
        $wxconfig->delete();
        return redirect('wx/wxconfig')->with('success', '删除成功');
    }
}
